//
//  CommunityViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/12.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "CommunityViewController.h"
#import "CommunityModel.h"
#import "CommuntityCollectionViewCell.h"
#import "CommunCollectionReusableView.h"
#import "CommunSecViewController.h"

@interface CommunityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
{
    NSMutableArray * _array;
    NSMutableArray * _groupArray;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation CommunityViewController

// 视图的出现将tabbar显示
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    
    // 设置navigationBar颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"社区";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    
    //解析数据
    [self createData];
    
    //collectionView
    [self createCollection];

    
}

//请求数据
-(void)createData
{
    
    [ZSMAFNetWork geturl:@"http://open.qyer.com/qyer/bbs/forum_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88262860611146&lon=121.5396766802357&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%25208.1.2&v=1%20HTTP/1.1" body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
        _array=[[NSMutableArray alloc]init];
        NSMutableDictionary *dicBegin = result;
        for (NSMutableDictionary *dicED in dicBegin[@"data"]) {
            CommunityModel *model = [[CommunityModel alloc]init];
            [model setValuesForKeysWithDictionary:dicED];
            [_array addObject:model];

        }
        [_collectionView reloadData];
        self.hud.hidden = YES;
        
    } failure:^(NSError *error) {
        
    }];
   
}

//创建collectionView
-(void)createCollection
{
    UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc]init];
    FlowLayout.itemSize = CGSizeMake(180 * WIDTH, 120 * HEIGHT );
    FlowLayout.minimumInteritemSpacing = 0;
    FlowLayout.minimumLineSpacing = 0;

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375,HEIGHT * 554)collectionViewLayout:FlowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.hud = [MBProgressHUD showHUDAddedTo:self.collectionView animated:YES];
    [self.hud hide:YES afterDelay:2];
    self.collectionView.emptyDataSetDelegate = self;
    self.collectionView.emptyDataSetSource = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];    
    //collectionView
    [self.collectionView registerClass:[CommuntityCollectionViewCell class] forCellWithReuseIdentifier:@"reuseStart"];
    //collectionView头视图
    [self.collectionView registerClass:[CommunCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseEnd"];

}

// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    //push到下一页
    CommunSecViewController *sec = [[CommunSecViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
    //model
    CommunityModel *model = _array[indexPath.section];
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSMutableDictionary *dic in model.group) {
        CommunityModel *zoon = [[CommunityModel alloc]init];
        [zoon setValuesForKeysWithDictionary:dic];
        [tempArr addObject:zoon];
        
    }
    
    //传id到下一页
    CommunityModel *mod = tempArr[indexPath.row];
    sec.UId = mod.uId;
    
}



//分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _array.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CommunityModel *model = _array[indexPath.section];
    NSMutableArray *arr = model.group;
    _groupArray = [NSMutableArray array];
    //解析group
    for (NSMutableDictionary *dic in arr) {
        CommunityModel *gModel = [[CommunityModel alloc]init];
        [gModel setValuesForKeysWithDictionary:dic];
        [_groupArray addObject:gModel];

    }
    //model
   CommunityModel *Region = _groupArray[indexPath.row];
    //collectionView 的 cell
    CommuntityCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseStart" forIndexPath:indexPath];
    
    //标题
    cell.labelName.text = Region.name;
    //有多少个主题
    cell.labelNumber.text = [NSString stringWithFormat:@"%@个主题",Region.total_threads];
    //图片
    [cell.image sd_setImageWithURL:[NSURL URLWithString:Region.photo]placeholderImage:[UIImage imageNamed:@"F"]];
    
    
    return cell;
}

//分区
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    CommunityModel *zoon = _array[section];
    return zoon.group.count;
}

//头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CommunityModel *zoon = _array[indexPath.section];
    CommunCollectionReusableView *head  = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"reuseEnd" forIndexPath:indexPath];
    head.labelBeat.text = zoon.name;
    
    return head;
    
}
// 大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(375 * WIDTH, 20 * HEIGHT);
}
// 上下间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20 * HEIGHT;
}
// 行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 20 * HEIGHT;
}
//每个视图的宽高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(140 * WIDTH, 40 * HEIGHT);
}
// 设置整个分区相对上下左右的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10 * HEIGHT, 20 * WIDTH, 10 * HEIGHT);
}


- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.collectionView.backgroundColor = [UIColor blackColor];
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
